﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sweepids.Models.Dao;
using Sweepids.Models.Object;

namespace Sweepids.Controllers
{
    public class ManageUsersController : Controller
    {
        //
        // GET: /ManageUser/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddUser()
        {
            return View();
        }
        public ActionResult DeleteUser()
        {
            try
            {
                //get All users
                var userDao = new UserDao();
                var users = userDao.ReadAll();
                var userlistFinal = new List<User>();
                //remove admin (they cannot be deleted as they can only be created directly in the database)
                foreach (var u in users)
                {
                    if (!u.IsAdmin)
                    {
                        userlistFinal.Add(u);
                    }
                }
                return View(userlistFinal);
            }
            catch (Exception)
            {
                TempData["Error"] = "Something went viewing the users to delete.";
                return RedirectToAction("AddUser", "ManageUsers");
            }
        }
        [HttpPost]
        public ActionResult DeleteTheUser(string users)
        {
            try
            {
                //get userdao
                var userDao = new UserDao();
                //remove user based off of username
                //create new user with this username and remove it
                var theUser = new User { Username = users };
                userDao.Delete(theUser);
                var userlistFinal = new List<User>();
                var userlist = userDao.ReadAll();
                foreach (var u in userlist)
                {
                    if (!u.IsAdmin)
                    {
                        userlistFinal.Add(u);
                    }
                }

                TempData["Success"] = "Successfully deleted the user.";
                return View("DeleteUser", userlistFinal);
            }
            catch (Exception)
            {
                TempData["Error"] = "Something went deleting the user.";
                return RedirectToAction("AddUser", "ManageUsers");
            }
        }
        [HttpPost]
        public ActionResult AddUser(string username, string password, string firstname, string lastname, string email, string address, string city, string state, string zip, string cardNumber, string expDate, string secCode)
        {
            try
            {
                var userDao = new UserDao();
                var zipDao = new ZipDao();
                var ccDao = new CreditCardDao();

                var dbZip = zipDao.Read(zip);

                // new zipcode
                if (dbZip.City.Equals(""))
                {
                    zipDao.Create(new Zip { City = city, State = state, ZipId = zip });
                }
                else if (!dbZip.City.Equals(city) || !dbZip.State.Equals(state))
                {
                    // problem, zip doesn't correspend to the state and city they entered in
                    TempData["Error"] = "The zip code did not match the city/state.";
                    return RedirectToAction("AddUser", "ManageUsers");
                }

                var dbCreditCard = ccDao.Read(cardNumber);

                // new card
                if (dbCreditCard.SecCode.Equals(""))
                {
                    ccDao.Create(new CreditCard { CardNum = cardNumber, SecCode = secCode, ExpDate = DateTime.Parse(expDate) });
                }
                else if (!dbCreditCard.SecCode.Equals(secCode))
                {
                    TempData["Error"] = "The credit card information does not match our records.";
                    return RedirectToAction("AddUser", "ManageUsers");
                }

                var newUser = new User
                {
                    FirstName = firstname,
                    LastName = lastname,
                    Address = address,
                    CreditCard = cardNumber,
                    Email = email,
                    IsAdmin = false,
                    SweepsOwned = 0,
                    Username = username,
                    Password = password,
                    Zip = zip,
                };

                var dbUser = userDao.Read(newUser.Username);
                if (dbUser.FirstName.Equals(""))
                {
                    userDao.Create(newUser);
                }
                else
                {
                    TempData["Error"] = "User already exists";
                    return RedirectToAction("AddUser", "ManageUsers");
                }


                TempData["Success"] = "Successfully added the user.";
                return RedirectToAction("AddUser", "ManageUsers");
            }
            catch (Exception e)
            {
                TempData["Error"] = "Something went wrong adding the user.";
                return RedirectToAction("AddUser", "ManageUsers");
            }
        }

    }
}
